package com.mxy.myapplication.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.bitmap.BitmapEncoder;
import com.mxy.myapplication.R;
import com.mxy.myapplication.activity.ActivityForCutp;
import com.mxy.myapplication.util.itemtouchhelperUtil.ItemTouchHelperAdapter;
import com.mxy.myapplication.util.itemtouchhelperUtil.ItemTouchHelperViewHolder;

import java.util.ArrayList;
import java.util.Collections;

/**
 * 网格布局RecyclerView适配器
 *
 * 实现ItemTouchHelperAdapter接口，实现ItemTouchHelper效果
 */

public class AdapterForRv_Grid extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements ItemTouchHelperAdapter{

    private Context cont;
    private LayoutInflater lf;
    private ArrayList<Integer> photolist=new ArrayList<>();
    private GridLayoutManager gridLayoutManager;
    private SwipeRefreshLayout swipeRefreshLayout;

    public AdapterForRv_Grid(Context cont,GridLayoutManager gridLayoutManager,SwipeRefreshLayout swipeRefreshLayout){
        this.cont=cont;
        this.lf=LayoutInflater.from(cont);
        this.gridLayoutManager=gridLayoutManager;
        this.swipeRefreshLayout=swipeRefreshLayout;
        photolist.add(R.drawable.jj1);
        photolist.add(R.drawable.jj2);
        photolist.add(R.drawable.jj3);
        photolist.add(R.drawable.bleach1);
        photolist.add(R.drawable.bleach2);
        photolist.add(R.drawable.bleach3);
        photolist.add(R.drawable.guanlan1);
        photolist.add(R.drawable.guanlan2);
        photolist.add(R.drawable.guanlan3);
//        gridLayoutManager.setSpanSizeLookup(new MySpanSizeLookup());//根据getItemViewType设置item在该行所占单元格的个数(item长度)
    }

    @Override   //item拖动时监听 执行N次  置换数据集位置，与UI界面保持一致
    public boolean onItemMove(int fromPosition, int toPosition) {
        Collections.swap(photolist, fromPosition, toPosition);
        notifyItemMoved(fromPosition, toPosition);
        return true;
    }

    @Override  //item被拖出recycler，作为item被删除监听   适用于linearLayoutManager
    public void onItemDismiss(int position) {
        photolist.remove(position);
        notifyItemRemoved(position);
    }

    /**
     * 设置每个item在该行所占单元格的个数(item长度)
     * header
     * footer
     */
    class MySpanSizeLookup extends GridLayoutManager.SpanSizeLookup{

        @Override
        public int getSpanSize(int position) {
            return getItemViewType(position)==0?gridLayoutManager.getSpanCount():1;//返回的是该item所占单元格的个数
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new MyViewHolder(this.lf.inflate(R.layout.item_recyclerview_grid,parent,false));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        MyViewHolder myViewHolder=(MyViewHolder) holder;
        myViewHolder.iv.setOnClickListener(new onclickListener(position));

        Glide.with(cont)
                .load(photolist.get(position))
                .asBitmap()
                .encoder(new BitmapEncoder(Bitmap.CompressFormat.PNG,100))
                .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                .dontAnimate()
                .animate(R.anim.alpha)
                .into(myViewHolder.iv);
    }

    @Override
    public int getItemCount() {
        return photolist.size();
    }

    /**
     * 自定义ViewHolder,实现ItemTouchHelperViewHolder接口，作为item选中/放开的监听方法
     */
    private class MyViewHolder extends RecyclerView.ViewHolder implements ItemTouchHelperViewHolder{

        ImageView iv;

        private MyViewHolder(View itemView) {
            super(itemView);
            iv=(ImageView) itemView.findViewById(R.id.imageview);
        }

        @Override
        public void onItemSelected() {//用户长安选中一个item
            swipeRefreshLayout.setEnabled(false);
            iv.setPadding(30,30,30,30);
        }

        @Override
        public void onItemClear() {//用户手指弹起
            swipeRefreshLayout.setEnabled(true);
            iv.setPadding(0,0,0,0);
        }
        //通常ItemTouchHelper效果都是通过长按item来触发
        //如果需要实现通过某个控件实现点击立刻拖动，可以通过该控件的ontouch事件，在里面执行itemTouchHelper.startDrag(viewHolder);方法即可
    }

    /**
     * 为item增加点击事件
     */
    private class onclickListener implements View.OnClickListener{

        int position;

        private onclickListener(int position){
            this.position=position;
        }

        @Override
        public void onClick(View v) {
            cont.startActivity(new Intent(cont, ActivityForCutp.class));
        }
    }
}